﻿package com.rragona.utils.coreXML
{
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	
	/**
	* ...
	* @author rragona
	* @version 1.0
	* 
	* This is a generic XML class that can be extended for more specific results.
	* 
	*/
	
	public class CoreXML extends URLLoader
	{
		private var _xmlLoader;
		private var _xmlRequest;
		private var _xmlData;
		
		/**
		 * @usage	This class accepts a single parameter - the path that you wish to load. 
		 * Example usage: var myVar:CoreXML = new CoreXML("myPath.xml");
		 * 
		 * @param	path	Path to the XML document that you wish to load
		 */ 
		
		public function CoreXML(path:String) 
		{
			_xmlLoader = new URLLoader();
			_xmlRequest = new URLRequest(path);
			
			initListeners();
			
			_xmlLoader.load(_xmlRequest);
		}
		
		public function get xmlData():XML
		{
			return _xmlData;
		}
		
		/* Listeners and Events */
		
		private function initListeners():void
		{
			_xmlLoader.addEventListener(Event.COMPLETE, postXMLLoad);
			_xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, catchIOError);
			_xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, catchSecurityViolation);
		}
		
		private function postXMLLoad(e:Event):void
		{
			_xmlData = new XML(e.target.data);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function catchIOError(event:IOErrorEvent):void
		{
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
		}
		
		private function catchSecurityViolation(e:SecurityErrorEvent):void
		{
			trace("CoreXML Security Violation Caught: " + e);
		}
		
	}
	
}